package com.example.wuhexian;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

public class CalculateActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
    EditText et1,et2;
    TextView addTv,jianTv,subTv,divTv;
    CheckBox addCb,jianCb,subCb,divCb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculate);
        initView();
        //设置监听器
        addCb.setOnCheckedChangeListener(this);
        jianCb.setOnCheckedChangeListener(this);
        subCb.setOnCheckedChangeListener(this);
        divCb.setOnCheckedChangeListener(this);
        //startCal();
    }


    private void initView() {
        et1 = findViewById(R.id.et1);
        et2 = findViewById(R.id.et2);
        addTv = findViewById(R.id.tv_add);
        jianTv = findViewById(R.id.tv_jian);
        subTv = findViewById(R.id.tv_sub);
        divTv = findViewById(R.id.tv_div);
        addCb = findViewById(R.id.cb_add);
        jianCb = findViewById(R.id.cb_jian);
        subCb = findViewById(R.id.cb_sub);
        divCb = findViewById(R.id.cb_div);
        //不显示结果信息
        addTv.setVisibility(View.INVISIBLE);
        jianTv.setVisibility(View.INVISIBLE);
        subTv.setVisibility(View.INVISIBLE);
        divTv.setVisibility(View.INVISIBLE);
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        switch (buttonView.getId()) {
            case R.id.cb_add:
                if (addCb.isChecked()) {
                    int i1 = Integer.parseInt(et1.getText().toString());
                    int i2 = Integer.parseInt(et2.getText().toString());
                    int res = i1+i2;
                    String msg = i1+"+"+i2+"="+res;
                    addTv.setText(msg);
                    addTv.setVisibility(View.VISIBLE);
                }else {
                    addTv.setVisibility(View.INVISIBLE);
                }
                break;
            case R.id.cb_jian:
                if (jianCb.isChecked()) {
                    int i1 = Integer.parseInt(et1.getText().toString());
                    int i2 = Integer.parseInt(et2.getText().toString());
                    int res = i1-i2;
                    String msg = i1+"-"+i2+"="+res;
                    jianTv.setText(msg);
                    jianTv.setVisibility(View.VISIBLE);
                }else {
                    jianTv.setVisibility(View.INVISIBLE);
                }
                break;
            case R.id.cb_sub:
                if (subCb.isChecked()) {
                    int i1 = Integer.parseInt(et1.getText().toString());
                    int i2 = Integer.parseInt(et2.getText().toString());
                    int res = i1*i2;
                    String msg = i1+"*"+i2+"="+res;
                    subTv.setText(msg);
                    subTv.setVisibility(View.VISIBLE);
                }else {
                    subTv.setVisibility(View.INVISIBLE);
                }
                break;
            case R.id.cb_div:
                if (divCb.isChecked()) {
                    int i1 = Integer.parseInt(et1.getText().toString());
                    int i2 = Integer.parseInt(et2.getText().toString());
                    int res = i1/i2;
                    String msg = i1+"/"+i2+"="+res;
                    divTv.setText(msg);
                    divTv.setVisibility(View.VISIBLE);
                }else {
                    divTv.setVisibility(View.INVISIBLE);
                }
                break;
        }
    }
}